









package com.company.z8.xingweileiPK.observer_vs_chain.src.com.company.observer;

import java.util.Observable;
import java.util.Observer;
import java.util.Random;


/**
 * @author cbf4Life cbf4life@126.com
 * I'm glad to share my knowledge with you all.
 * ÿ��DNS����������ʹ�۲���Ҳ�Ǳ��۲���
 */
public abstract class DnsServer extends Observable implements Observer {

	
	public void update(Observable arg0, Object arg1) {
		Recorder recorder = (Recorder)arg1;
		
		if(isLocal(recorder)){
			recorder.setIp(genIpAddress());
		}else{
			responsFromUpperServer(recorder);
		}
		
		sign(recorder);
	}

	
	public void setUpperServer(DnsServer dnsServer){
		
		super.deleteObservers();
		super.addObserver(dnsServer);
	}
	
	
	private void responsFromUpperServer(Recorder recorder){
		super.setChanged();
		super.notifyObservers(recorder);
	}
	
	
	protected abstract void sign(Recorder recorder);
	

	
	
	protected abstract boolean isLocal(Recorder recorder);
	
	
	private String genIpAddress(){
		Random rand = new Random();
		String address = rand.nextInt(255) + "." + rand.nextInt(255) + "."+ rand.nextInt(255) + "."+ rand.nextInt(255);
		
		return address;
	}
}